package com.example.jacksen.alldemo.Rxbus.Rxjava;

import android.nfc.Tag;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/7/1.
 */

public class RxUtils {
    private static final String TAG = RxUtils.class.getSimpleName();
    //使用onCreate
    public static void createObserable(){
        //定义被观察着
        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                if (!subscriber.isUnsubscribed()){
                    subscriber.onNext("hello");
                    subscriber.onNext("w");
                    subscriber.onNext(downLoadJson());
                    subscriber.onNext("wold");
                    subscriber.onCompleted();
                }
            }
        });
        Subscriber<String>  stringSubscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError"+e.getMessage());
            }

            @Override
            public void onNext(String s) {
                Log.i(TAG,"result---->"+s);
            }
        };
        //关联被观察着
        observable.subscribe(stringSubscriber);
    }
    public static String downLoadJson(){
        return "json data";
    }
    public static void createPrint(){
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                if (!subscriber.isUnsubscribed()){
                    for (int i=1;i<=18;i++){
                        subscriber.onNext(i);
                    }
                    subscriber.onCompleted();
                }
            }
        }).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError"+e.getMessage());
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG,"result---->"+integer);
            }
        });
    }

    /**
     * form数据展示
     */
    public static void from(){
        final Integer[] items = {1,2,3,4,5,6,7,8,9};
        Observable observable = Observable.from(items);
        observable.subscribe(new Action1() {
            @Override
            public void call(Object o) {
                Log.i(TAG,o.toString());
            }
        });
    }
    /**
     * 指定某一时刻进行数据发送
     */
    public static void interval(){
        Integer[] items ={100,101,102,103,104,105};
        //每隔一秒发送数据
        Observable observable = Observable.interval(1,1, TimeUnit.SECONDS);
        observable.subscribe(new Action1() {
            @Override
            public void call(Object o) {
                Log.i(TAG,"每秒数据"+o.toString());
            }
        });
    }
    /**
     *just数组的处理
     */
    public static  void just(){
        Integer[] item1 = {1,2,4,5,6};
        Integer[] item2 = {3,7,8,9,0};
        Observable observable = Observable.just(item1,item2);
        observable.subscribe(new Subscriber<Integer[]>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError"+e.getMessage());
            }

            @Override
            public void onNext(Integer[] o) {
                for (int i=0;i<o.length;i++){
                    Log.i(TAG,"result---->"+o[i]);
                }

            }
        });
    }
    /**
     * 指定输出数据的范围
     */

    public static void range(){
        Observable observable = Observable.range(1,48);
        observable.subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError"+e.getMessage());
            }

            @Override
            public void onNext(Integer o) {
                Log.i(TAG,"result--->"+o.toString());
            }
        });
    }

    /**
     * 过滤
     */
    public static void filter(){
        Observable observable = Observable.just(1,2,3,4,5,6,7);
        observable.filter(new Func1<Integer,Boolean>() {
            @Override
            public Boolean call(Integer o) {
                return o<5;
            }
        }).observeOn(Schedulers.io()).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError"+e.getMessage());
            }

            @Override
            public void onNext(Integer o) {
                Log.i(TAG,"result--->"+o.toString());
            }
        });
    }
}
